package com.example.home.pet;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.home.R;
import com.example.home.pet.adapter.CaseInquiryAdapter;
import com.example.home.pet.adapter.MeInquiryAdapter;
import com.example.home.pet.adapter.PetCartAdapter;
import com.example.home.pet.bean.CaseInquiryBean;
import com.example.home.pet.bean.MeInquiryBean;
import com.example.home.pet.bean.PetCartBean;
import com.example.home.tool.OKHttpTool;

public class PetMainActivity extends AppCompatActivity {

    private ImageView back;
    private RecyclerView petCart;
    private RecyclerView rcvMeInquiry;
    private RecyclerView rcvCaseInquiry;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pet_main);
        initView();
        //宠物分类
        getPetCart();
        //我的问诊
        getMeInquiry();//暂无记录
        //问诊案例
        getCaseInquiry();
    }

    private void getCaseInquiry() {
        OKHttpTool.doNetRequest("prod-api/api/pet-hospital/inquiry/list?pageNum=1&pageSize=10",
                OKHttpTool.GET,
                CaseInquiryBean.class,
                new OKHttpTool.NetCallback() {
                    @Override
                    public void onSuccess(Object obj) {
                        CaseInquiryBean caseInquiryBean = (CaseInquiryBean) obj;
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                bindCase(caseInquiryBean);
                            }
                        });
                    }

                    @Override
                    public void onFailure(String msg) {

                    }
                });
    }

    private void bindCase(CaseInquiryBean caseInquiryBean) {
        CaseInquiryAdapter caseInquiryAdapter = new CaseInquiryAdapter(PetMainActivity.this,caseInquiryBean.getRows());
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(PetMainActivity.this);
        rcvCaseInquiry.setAdapter(caseInquiryAdapter);
        rcvCaseInquiry.setLayoutManager(linearLayoutManager);
    }

    private void getMeInquiry() {
        OKHttpTool.doNetRequest("prod-api/api/pet-hospital/inquiry/my-list",
                OKHttpTool.GET,
                MeInquiryBean.class,
                new OKHttpTool.NetCallback() {
                    @Override
                    public void onSuccess(Object obj) {
                        MeInquiryBean meInquiryBean = (MeInquiryBean) obj;
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                bindMeInquiry(meInquiryBean);
                            }
                        });
                    }

                    @Override
                    public void onFailure(String msg) {

                    }
                });
    }

    private void bindMeInquiry(MeInquiryBean meInquiryBean) {
        MeInquiryAdapter caseInquiryAdapter = new MeInquiryAdapter(PetMainActivity.this,meInquiryBean.getRows());
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(PetMainActivity.this);
        rcvMeInquiry.setAdapter(caseInquiryAdapter);
        rcvMeInquiry.setLayoutManager(linearLayoutManager);
    }

    private void getPetCart() {
        OKHttpTool.doNetRequest("prod-api/api/pet-hospital/pet-type/list",
                OKHttpTool.GET,
                PetCartBean.class,
                new OKHttpTool.NetCallback() {
                    @Override
                    public void onSuccess(Object obj) {
                        PetCartBean petCartBean = (PetCartBean) obj;
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                bindCart(petCartBean);
                            }
                        });
                    }

                    @Override
                    public void onFailure(String msg) {

                    }
                });
    }

    private void bindCart(PetCartBean petCartBean) {
        PetCartAdapter petCartAdapter = new PetCartAdapter(PetMainActivity.this,petCartBean.getData());
        GridLayoutManager gridLayoutManager = new GridLayoutManager(PetMainActivity.this,5);
        petCart.setAdapter(petCartAdapter);
        petCart.setLayoutManager(gridLayoutManager);
    }

    private void initView() {

        back = findViewById(R.id.back);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                PetMainActivity.this.finish();
            }
        });

        petCart = findViewById(R.id.pet_cart);
        rcvMeInquiry = findViewById(R.id.rcv_meInquiry);
        rcvCaseInquiry = findViewById(R.id.rcv_caseInquiry);

    }
}